package com.agil.photoalbum.web.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.agil.photoalbum.db.dao.Functions;

/**
 * Servlet implementation class for Servlet: SendMailServlet
 *
 */
 public class SendMailServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SendMailServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String picId = request.getParameter("picId");
		String userId = request.getParameter("userId");
		String recepientMail = request.getParameter("recepientMail");
		String subject = request.getParameter("subject");
		String msgText = request.getParameter("msgText");
		
		if(Functions.sendMail(picId, userId, recepientMail, subject, msgText)){
			request.setAttribute("sendingStatus", "Email sent successfully");
			request.getRequestDispatcher("pages/sendMailForm.jsp");
		} else {
			request.setAttribute("sendingStatus", "Sending mail failed. Please try again.");
			request.getRequestDispatcher("pages/sendMailForm.jsp");
		}
		
	}   	  	    
}